package org.tinygroup.parsedsql.visitor;

import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.parsedsql.exception.ParsedSqlException;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;

/**
 * MySQL的UPDATE语句访问器.
 * 
 * @author renhui
 */
public class MySQLUpdateVisitor extends AbstractMySQLVisitor {
	
	private int itemIndex=0;
	private int itemSize;
	private int processNum;//去除更新项数目
	
	public boolean visit(final MySqlUpdateStatement x) {
		print("UPDATE ");

        if (x.isLowPriority()) {
            print("LOW_PRIORITY ");
        }

        if (x.isIgnore()) {
            print("IGNORE ");
        }

        x.getTableSource().accept(this);

        println();
        print("SET ");
        itemSize=x.getItems().size();
        for (int i = 0, size = x.getItems().size(); i < size; ++i) {
//            if (i != 0) {
//                print(", ");
//            }
            x.getItems().get(i).accept(this);
            itemIndex++;
        }
        if(processNum==itemSize){
        	throw new ParsedSqlException("sql is incorrect：no update item exist");
        }
        if (x.getWhere() != null) {
            println();
            incrementIndent();
            print("WHERE ");
            x.getWhere().setParent(x);
            x.getWhere().accept(this);
            decrementIndent();
        }

        if (x.getOrderBy() != null) {
            println();
            x.getOrderBy().accept(this);
        }

        if (x.getLimit() != null) {
            println();
            x.getLimit().accept(this);
        }
        return false;
	}
	
	

	public boolean visit(final SQLUpdateSetItem x) {
		SQLExpr value = x.getValue();
		if(value instanceof SQLVariantRefExpr){
			SQLVariantRefExpr refExpr=(SQLVariantRefExpr)value;
			String refName=refExpr.getName();
			boolean exist = getContext().exist(StringUtil.substringAfter(refName, "@"));
			if(itemIndex!=0&&exist){
				  print(", ");
			}
			if(!exist){
				processNum++;
				print(" ");
				return false;
			}
		}
		  super.visit(x);
		  return false;
	}

}
